﻿using Susha.Areas.Admin.Models;
using Susha.Areas.Admin.Models.Requests;
using Susha.Areas.Admin.Models.Pages;
using Susha.Models;
using Susha.Models.Helpers;
using Susha.Models.Pages;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;

namespace Susha.Areas.Admin.Controllers
{
    public class MenuController : Controller
    {
        //
        // GET: /Admin/Menu/

        [Authorize(Roles="Admin")]
        public ActionResult Index()
        {
            MenuPageAdmin page = new MenuPageAdmin();
            page.Categories = CategoryRepository.GetAllCategories();

            return View(page);
        }

        //[Authorize(Roles = "Admin")]
        public ActionResult Recreate()
        {
            CommonTools.ResetDB(true);
            return RedirectToAction("Index", "Menu", new { area = "Default" });
        }

        [HttpPost]
        [Authorize(Roles = "Admin")]
        public String ChangeSushiName(SushiChangeName sushi)
        {
            String result = String.Empty;
            if (SushiRepository.ChangeSushiName(sushi.Id, sushi.Name))
            {
                result = "Ok";
            }
            return result;
        }

        [HttpPost]
        [Authorize(Roles = "Admin")]
        public String ChangePrice(SushiChangePrice sushi)
        {
            String result = String.Empty;
            if (SushiRepository.ChangePrice(sushi.Id, sushi.Price))
            {
                result = "Ok";
            }
            return result;
        }

        [HttpPost]
        [Authorize(Roles = "Admin")]
        public String ChangeCategoryName(CategoryChangeName category)
        {
            String result = String.Empty;
            if (CategoryRepository.ChangeCategoryName(category.Id, category.Name))
            {
                result = "Ok";
            }
            return result;
        }

        [HttpPost]
        [Authorize(Roles = "Admin")]
        public JsonResult CreateSushi(SushiCreate sushi)
        {
            Sushi s = sushi.Convert2Sushi();
            s = SushiRepository.Save(s);
            s.ParentCategory = null;
            return Json(s);
        }

        [HttpPost]
        [Authorize(Roles = "Admin")]
        public String DeleteSushi(TransmitId sushiInfo)
        {
            return SushiRepository.DeleteSushi(sushiInfo.Id);
        }

        [HttpPost]
        [Authorize(Roles = "Admin")]
        public JsonResult CreateNewCategory(Category category)
        {
            ///
            /// Check for existing Category
            /// 
            category = CategoryRepository.Save(category);
            return Json(category);
        }

        [HttpPost]
        [Authorize(Roles = "Admin")]
        public String DeleteCategory(TransmitId categoryId)
        {
            return CategoryRepository.DeleteCategory(categoryId.Id);
        }

        [HttpPost]
        [Authorize(Roles = "Admin")]
        public ActionResult UploadImage(SushiChangePhoto photo)
        {
            if (photo != null && photo.SushiPhoto != null)
            {
                    using(ImageTools it = new ImageTools(photo.SushiPhoto))
                    {
                        String newPhotoFileName = it.SaveFile(PagesEnum.Menu, 220, 135);
                        SushiRepository.ChangePhoto(photo.SushiId, newPhotoFileName);
                    }
                
            }
            return RedirectToAction("Index");
        }

        [HttpPost]
        [Authorize(Roles = "Admin")]
        public JsonResult ChangeSushiPlace(SushiChangePlace sushiData)
        {

            return Json(sushiData);
        }
    }
}
